package com.wtwd.wtwddevice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.wtwddevice.mapper.SetSosMapper;
import com.wtwd.wtwddevice.model.entity.SetSos;
import com.wtwd.wtwddevice.service.ISetSosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetSosServiceImpl extends ServiceImpl<SetSosMapper, SetSos> implements ISetSosService {

    @Autowired
    private SetSosMapper setSosMapper;

    /**
     * 一台设备只有一个sos号码 重复添加会覆盖
     * 批量修改
     * 先改成修改最后一个
     * @param list
     * @return
     */
    @Override
    public int batchEditSetSosButton(List<SetSos> list) {
        int editSuccess = 0;
        if(!list.isEmpty()){
            SetSos sos = list.get(list.size() - 1);
            SetSos setSosByDeviceId = setSosMapper.getSetSosByDeviceId(sos.getDeviceId());
            if(setSosByDeviceId == null){
                setSosMapper.insertSetSos(sos);
            }else{
                editSuccess = setSosMapper.editSetSos(sos);
            }
        }
        return editSuccess;
    }

    @Override
    public int editState(Long deviceId, String number, int state) {
        return setSosMapper.editState(deviceId,number,state);
    }
}
